package com.kanshu.ksgb.fastread.doudou.common.net.retrofit;

import android.text.TextUtils;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.Xutils;

import java.util.Collections;

import okhttp3.CipherSuite;
import okhttp3.ConnectionSpec;
import okhttp3.OkHttpClient;
import okhttp3.TlsVersion;

/**
 * 简单retrofit类
 * Created by wcy on 2017/4/20.
 */
public class SimpleRetrofit extends BaseRetrofit {
    private String mHost;

    public static SimpleRetrofit getInstance() {
        return getInstance(SimpleRetrofit.class, SimpleRetrofit::new);
    }

    public SimpleRetrofit() {
    }

    @Override
    protected String getBaseUrl() {
        if (!TextUtils.isEmpty(mHost)) {
            return mHost;
        }
        String host = Xutils.getServerConfig("MAIN_HOST");
        if (TextUtils.isEmpty(host)) {
            host = Xutils.getContext().getString(R.string.host);
        }
        mHost = host;
        return host;
    }

    @Override
    protected OkHttpClient.Builder wrapOkHttpBuilder(OkHttpClient.Builder builder) {
        handleSsl(builder);
        return builder;
    }

    private void handleSsl(OkHttpClient.Builder builder) {
        ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
                .tlsVersions(TlsVersion.TLS_1_1)
                .tlsVersions(TlsVersion.TLS_1_2)
                .tlsVersions(TlsVersion.TLS_1_0)
                .cipherSuites(
                        CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
                        CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
                        CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
                        CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
                        CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
                        CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA,
                        CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256)
                .build();
        builder.connectionSpecs(Collections.singletonList(spec));
    }


}
